#!/usr/bin/env python
import os
import sys
from glob import glob
from astropy.io import fits

mtf_dir = sys.argv[1]
dark_flag = int(sys.argv[2])
if dark_flag == 1:
    dark_dir = sys.argv[3]
    dark_files = glob(dark_dir+"/*.bmp")
    for i in range(len(dark_files)):
        os.system("bmp2fits_wgh.py "+dark_files[i]+" "+dark_files[i][:-4]+".fits")
    dark_files = glob(dark_dir+"/*.fits")
    for i in range(len(dark_files)):
        if i==0:
            dark_data = fits.open(dark_files[i])[0].data/len(dark_files)
        else:
            dark_data += fits.open(dark_files[i])[0].data/len(dark_files)
        fits.writeto("dark_main.fits",data=dark_data,overwrite=True)
        
mtf_files = glob(mtf_dir+"/*.bmp")
for i in range(len(mtf_files)):
    os.system("bmp2fits_wgh.py "+mtf_files[i]+" "+mtf_files[i][:-4]+".fits")
